package com.semptian.deye.semptian.analyzer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.analysis.Tokenizer;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.analysis.AbstractTokenizerFactory;

import java.io.File;
import java.nio.file.Path;

/**
 * @author zww
 * @date 2021/8/12 20:20
 * @desc
 */
public class SemptianTokenizerFactory extends AbstractTokenizerFactory {
    private final static Logger LOG = LogManager.getLogger(SemptianTokenizerFactory.class);
    private final SemptianTokenizer tokenizer;


    public SemptianTokenizerFactory(IndexSettings indexSettings, Environment environment, String ignored, Settings settings) {
        super(indexSettings, ignored, settings);
        Path configPath = PathUtils.get(new File(SemptianAnalyzer.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent()).toAbsolutePath().resolve("jieba_config.properties");
        LOG.info("开始加载配置文件:[{}]", configPath.toAbsolutePath());
        tokenizer = new SemptianTokenizer();
    }

    @Override
    public Tokenizer create() {
        return tokenizer;
    }
}
